﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Markup;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TfsBugger.Core;
using TfsBugger.Util;

namespace TfsBugger
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        #region Fields
        private static Set<TfsServerManager> m_servers = new Set<TfsServerManager>();
        private static Set<Bugger> m_buggers = new Set<Bugger>();
        #endregion


        #region Common Properties/Objects
        private static Mutex SingleInstanceHandle { get; set; }

        public static TfsServerManager[] Servers { get { return m_servers.ToArray(); } }

        public static AppShell Shell { get; set; }

        public static Bugger[] Buggers
        {
            get { return m_buggers.ToArray(); }
        }
        #endregion


        #region Common Methods 
        private static string GetServerStorageFilePath()
        {
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "servers.xaml");
        }


        public static void RegisterServer(TfsServerManager svr)
        {
            m_servers.Add(svr);
            //string fileName = GetServerStorageFilePath();
            //if (File.Exists(fileName))
            //    File.Delete(fileName);

            //using (TextWriter tw = File.CreateText(fileName))
            //    XamlWriter.Save(m_servers, tw);
        }


        private void LoadServers()
        {
            //string fileName = GetServerStorageFilePath();
            //if (!File.Exists(fileName))
            //    return;

            //using (FileStream fs = File.OpenRead(fileName))
            //    m_servers = (Set<TfsServerManager>)XamlReader.Load(fs);
        }


        public static void RegisterBugger(Bugger b)
        {
            if (!m_buggers.TryAdd(b))
                return;

            CommonEvents.RaiseBuggerAdded(null, EventArgs.Empty);
        }


        public static void UnRegisterBugger(Bugger b)
        {
            if (!m_buggers.TryRemove(b))
                return;

            CommonEvents.RaiseBuggerRemoved(null, EventArgs.Empty);
        }
        #endregion


        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            LoadServers();


            //Enforce single-instance app
            //SingleInstanceHandle = new Mutex(false, Marshal.GetTypeLibGuidForAssembly(Assembly.GetExecutingAssembly()).ToString());
            //if (SingleInstanceHandle.WaitOne(0, false))
            //{
            //    var shell = new AppShell();
            //    Shell = shell;
            //}
        }


        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            foreach (Bugger b in Buggers)
            {
                if (b.IsBusy)
                    b.CancelAsync();
            }

            foreach (TfsServerManager svr in Servers)
            {
                if (null != svr)
                    svr.Server.Dispose();
            }

            //SingleInstanceHandle.Close();
            //base.OnExit(e);
        }
    }
}
